//
//  WZMineInfoCell.m
//  ETong
//
//  Created by wanzhao on 15/11/2.
//  Copyright (c) 2015年 wanzhao. All rights reserved.
//

#import "WZMineInfoCell.h"

@interface WZMineInfoCell ()<UITextFieldDelegate>

@property (nonatomic, strong) NSArray *telArray;


@end

@implementation WZMineInfoCell


- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}
+ (instancetype)mineInfoCellWithTableView:(UITableView *)tableView
{
    static NSString *mineId = @"mineInfoCell";
    WZMineInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:mineId];
    if (cell == nil) {
        cell = [[WZMineInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:mineId];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
                
        UIButton *iconBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        iconBtn.backgroundColor = ClearColor;
        [self.contentView addSubview:iconBtn];
        self.iconButton = iconBtn;
        
        UITextField *titLabel = [[UITextField alloc] init];
        UIView *view = [[UIView alloc] init];
        view.backgroundColor = ClearColor;
        view.frame = CGRectMake(0, 0, OppositeWidthRate(5), OppositeHeightRate(5));
        titLabel.leftView = view;
        titLabel.delegate = self;
        titLabel.leftViewMode = UITextFieldViewModeAlways;
        titLabel.borderStyle = UITextBorderStyleNone;
        titLabel.clearButtonMode = UITextFieldViewModeWhileEditing;
        titLabel.returnKeyType = UIReturnKeyDone;
        [self.contentView addSubview:titLabel];
        self.titField = titLabel;
        
        UIView *line = [[UIView alloc] init];
        line.backgroundColor = RGBCOLORWITH16COLOR(0xcccccc);
        [self.contentView addSubview:line];
        self.line = line;
        
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textViewDidChange) name:UITextFieldTextDidChangeNotification object:nil];
    }
    return self;
}

- (void)setTextViewTag:(TextViewTag)textViewTag
{
    _textViewTag = textViewTag;
    switch (textViewTag) {
        case TextViewMailTag:
        {
            self.titField.keyboardType = UIKeyboardTypeASCIICapable;
            self.titField.autocorrectionType = UITextAutocorrectionTypeNo;
        }
            break;
        case TextViewTelPhoneTag:
        {
            self.titField.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
            self.titField.autocorrectionType = UITextAutocorrectionTypeNo;
        }
            break;
        case TextViewMobileTag:
        {
            self.titField.keyboardType = UIKeyboardTypeNumberPad;
        }
            break;

        default:
            break;
    }}
- (void)setIsEdit:(BOOL)isEdit
{
    _isEdit = isEdit;
    if (isEdit) {
        self.titField.userInteractionEnabled = YES;
        self.titField.backgroundColor = RGBCOLOR(238, 238, 238);
        self.line.hidden = YES;
    } else {
        self.titField.userInteractionEnabled = NO;
        self.titField.backgroundColor = ClearColor;
        self.line.hidden = NO;
    }
}
- (void)textViewDidChange
{
    if ([self.InfoCellDelegate respondsToSelector:@selector(textViewDidChangeWithText:textViewTag:)]) {
        [self.InfoCellDelegate textViewDidChangeWithText:self.titField.text textViewTag:self.textViewTag];
    }
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    switch (self.textViewTag) {
        case TextViewTelPhoneTag:
        {
            if ([string isEqualToString: @"\n"]) {
                // Hide the keyboard
                [textField resignFirstResponder];
                
                // Also return if its showing a placeholder
                return NO ;
            }
            
            if ([string isEqualToString:@""]) {
                return YES;
            }

            if (textField.text.length > 19) {
                return NO;
            }
            
            if ([self isAvailbleTelWithStr:string]) {
                return YES;
            } else {
                return NO;
            }
        }
        case TextViewMobileTag:{
            if ([string isEqualToString: @"\n"]) {
                // Hide the keyboard
                [textField resignFirstResponder];
                
                // Also return if its showing a placeholder
                return NO ;
            }
            if ([string isEqualToString:@""]) {
                return YES;
            }
            
            if (textField.text.length > 10) {
                return NO;
            }
            
        }
        case TextViewMailTag:{
            if ([string isEqualToString: @"\n"]) {
                // Hide the keyboard
                [textField resignFirstResponder];
                
                // Also return if its showing a placeholder
                return NO ;
            }
            if ([string isEqualToString:@""]) {
                return YES;
            }
            
            if (textField.text.length > 49) {
                return NO;
            }
            
        }
    }
    
    
    
    return YES ;
    
}


- (BOOL)isAvailbleTelWithStr:(NSString *)str
{
    
    NSString *telRegex = @"(\\d|-)?";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",telRegex];
    //    NSLog(@"phoneTest is %@",phoneTest);
    return [phoneTest evaluateWithObject:str];

}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
#pragma mark UItextFieldDelegate

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.iconButton.frame = CGRectMake(OppositeWidthRate(16), 0, OppositeWidthRate(30), self.height);
    self.titField.frame = CGRectMake(CGRectGetMaxX(self.iconButton.frame) + OppositeWidthRate(24), OppositeHeightRate(5), self.width - CGRectGetMaxX(self.iconButton.frame) - OppositeWidthRate(48), self.height - OppositeHeightRate(10));
    self.line.frame = CGRectMake(OppositeWidthRate(16), self.height - OppositeHeightRate(1) / 2.0f, self.width - OppositeWidthRate(16) - OppositeWidthRate(8), OppositeHeightRate(0.5));
    
}

@end
